package restaurant.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

/**
 * This JPanel is used to view and edit the restaurant's menu.
 * 
 * @author Odessa
 */
@SuppressWarnings("serial")
public class MenuPanel extends JPanel
{

	/** The frame. */
	private JFrame frame;

	/** The price dialog. */
	private ViewMenuDialog priceDialog;

	/** The preparation time dialog. */
	private ViewMenuDialog prepTimeDialog;

	/** The rating dialog. */
	private ViewMenuDialog ratingDialog;

	/** The course combo box. */
	@SuppressWarnings("rawtypes")
	private JComboBox courseComboBox;

	/** The selected course. */
	private String courseSelected;



	/**
	 * Create the panel and add components.
	 * 
	 * @param frame the frame.
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public MenuPanel(JFrame frame) {
		this.frame = frame;

		BorderLayout borderLayout = new BorderLayout();
		borderLayout.setVgap(30);
		borderLayout.setHgap(20);

		setBorder(new EmptyBorder(0, 5, 0, 5));
		setPreferredSize(new Dimension(198, 200));

		// Create the dialog boxes
		priceDialog = new ViewMenuDialog(frame, "Enter maximum price below",
				"Price submittal", ViewMenuDialog.PRICE);
		priceDialog.pack();

		prepTimeDialog = new ViewMenuDialog(frame,
				"Enter maximum preparation time", "Preparation time submittal",
				ViewMenuDialog.PREP);
		prepTimeDialog.pack();

		ratingDialog = new ViewMenuDialog(frame, "Enter minimum item rating",
				"Rating submittal", ViewMenuDialog.RATING);
		ratingDialog.pack();

		// Create and set the grid bag layout
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 0.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		// Create and set the labels
		JLabel menuLabel = new JLabel("Menu");
		menuLabel.setVerticalAlignment(SwingConstants.TOP);
		menuLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
		GridBagConstraints gbc_menuLabel = new GridBagConstraints();
		gbc_menuLabel.ipady = 10;
		gbc_menuLabel.gridwidth = 3;
		gbc_menuLabel.insets = new Insets(0, 0, 5, 5);
		gbc_menuLabel.gridx = 0;
		gbc_menuLabel.gridy = 0;
		add(menuLabel, gbc_menuLabel);

		JLabel viewByLabel = new JLabel("View Menu");
		viewByLabel.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_viewByLabel = new GridBagConstraints();
		gbc_viewByLabel.ipadx = 5;
		gbc_viewByLabel.anchor = GridBagConstraints.EAST;
		gbc_viewByLabel.insets = new Insets(0, 0, 0, 5);
		gbc_viewByLabel.gridx = 0;
		gbc_viewByLabel.gridy = 2;
		add(viewByLabel, gbc_viewByLabel);

		// Create and set the combo box
		JComboBox viewByComboBox = new JComboBox();
		GridBagConstraints gbc_viewByComboBox = new GridBagConstraints();
		gbc_viewByComboBox.insets = new Insets(0, 0, 0, 5);
		gbc_viewByComboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_viewByComboBox.gridx = 1;
		gbc_viewByComboBox.gridy = 2;
		add(viewByComboBox, gbc_viewByComboBox);

		// Add items to the combo box
		viewByComboBox.addItem("Entire menu");
		viewByComboBox.addItem("By course");
		viewByComboBox.addItem("By price");
		viewByComboBox.addItem("By preparation time");
		viewByComboBox.addItem("By rating");
		viewByComboBox.addItem("Vegetarian menu");
		viewByComboBox.setSelectedIndex(-1);

		// Add action listener to the view by combo box
		viewByComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				viewByComboBoxActionPerformed(e);
			}
		});

		// Create the course combo box
		String[] courseOptions = { "Drinks", "Entree", "Main Course", "Dessert" };
		courseComboBox = new JComboBox(courseOptions);
		courseComboBox.setSelectedIndex(0);
		courseComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				JComboBox cb = (JComboBox) e.getSource();
				courseSelected = (String) cb.getSelectedItem();
				// System.out.println(courseSelected);
			}
		});

	}



	/**
	 * The action listener for the {@code viewByComboBox}. Gets the selected
	 * item from the combo box and opens a dialog box based on the choice
	 * chosen. The dialog box gets further info from the user before they can be
	 * shown the menu.
	 * 
	 * @param evt the action event.
	 */
	@SuppressWarnings("rawtypes")
	private void viewByComboBoxActionPerformed(ActionEvent evt)
	{
		JComboBox cb = (JComboBox) evt.getSource();
		String choice = (String) cb.getSelectedItem();

		// Entire menu
		if (choice.equals("Entire menu"))
			Restaurant.actions.getMenu();
		// System.out.println("Entire menu");
		else if (choice.equals("By course")) { // By course
			// System.out.println("By course");

			createOptionPane();
			Restaurant.actions.getMenuByCourse(courseSelected);
		}
		else if (choice.equals("By price")) { // By price
			priceDialog.setLocationRelativeTo(frame);
			priceDialog.setVisible(true);

			String s = priceDialog.getValidatedText();

			if (s != null) {
				// valid
				int price = Integer.parseInt(s);
				Restaurant.actions.getMenuByPrice(price);
				// System.out.println("Valid price: " + s);
			}
		}
		else if (choice.equals("By preparation time")) { // By preparation time
			prepTimeDialog.setLocationRelativeTo(frame);
			prepTimeDialog.setVisible(true);

			String s = prepTimeDialog.getValidatedText();

			if (s != null) {
				// valid
				int time = Integer.parseInt(s);
				Restaurant.actions.getMenuByPrepTime(time);
				// System.out.println("Valid prep time: " + s);
			}
		}
		else if (choice.equals("By rating")) { // By rating
			ratingDialog.setLocationRelativeTo(frame);
			ratingDialog.setVisible(true);

			String s = ratingDialog.getValidatedText();

			if (s != null) {
				// valid
				int rating = Integer.parseInt(s);
				Restaurant.actions.getMenuByRating(rating);
				// System.out.println("Valid rating: " + s);
			}
		}
		else
			// By vegetarian
			Restaurant.actions.getMenuByVegetarian();
		// System.out.println("Veggie menu");
	}



	/**
	 * Creates the option pane in order to create a dialog box to selected the
	 * course.
	 */
	private void createOptionPane()
	{
		String message = "Select a course from the combo box below to view the menu";
		Object[] array = { new JLabel(message), courseComboBox };
		Object[] options = { "Enter", "Cancel" };

		final JOptionPane optionPane = new JOptionPane(array,
				JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION,
				Restaurant.QUESTION_ICON, options, options[0]);

		final JDialog dialog = new JDialog(frame, "Course selection", true);
		dialog.setContentPane(optionPane);
		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

		optionPane.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent e)
			{
				String prop = e.getPropertyName();

				if (dialog.isVisible() && (e.getSource() == optionPane) &&
						(JOptionPane.VALUE_PROPERTY.equals(prop))) {
					// If you were going to check something
					// before closing the window, you'd do
					// it here.
					dialog.setVisible(false);
				}
			}
		});

		dialog.pack();
		dialog.setLocationRelativeTo(frame);
		dialog.setVisible(true);
	}



	/**
	 * Creates the GUI and show it.
	 */
	private static void createAndShowGUI()
	{
		JFrame frame = new JFrame("MenuPanel");

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(new MenuPanel(frame), BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
	}



	/**
	 * The main method creates and shows the GUI.
	 * 
	 * @param args the arguments.
	 */
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable() {
			public void run()
			{
				UIManager.put("swing.boldMetal", Boolean.FALSE);
				createAndShowGUI();
			}
		});
	}

}
